package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.TeacherService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.RoleEnum;
import com.thesis.manage.service.model.StateCodeEnum;
import com.thesis.manage.service.module.dto.LoginDTO;
import com.thesis.manage.service.module.dto.UserLoginDTO;
import com.thesis.manage.service.module.vo.LoginVO;
import com.thesis.teacher.feign.UserService;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/2/9
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private TeacherService teacherService;

    @Resource
    private UserService userService;

    /**
     * 用户信息详情
     */
    @GetMapping("/info")
    public Map<String, Object> info() {
        return buildMessage(() -> {
            Result user = getUser();
            Teacher teacher = JSONObject.parseObject(user.getData().toString(), Teacher.class);
            return teacherService.info(teacher.getId());
        });
    }

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody LoginDTO loginDTO) {
        return buildMessage(() -> {
            Result result = userService.login(loginDTO.getUsername(), loginDTO.getPassword(), RoleEnum.TEACHER.getValue());
            LoginVO loginVO = new LoginVO();
            if (result != null && result.getStatus() == StateCodeEnum.SUCCESS.getValue().intValue()) {
                Object data = result.getData();
                UserLoginDTO userLoginDTO = JSONObject.parseObject(data.toString(), UserLoginDTO.class);
                Teacher teacher = JSONObject.parseObject(userLoginDTO.getUserInfo(), Teacher.class);
                loginVO.setUsername(teacher.getName());
                loginVO.setImage(teacher.getImage());
                loginVO.setToken(userLoginDTO.getToken());
                loginVO.setRoles(userLoginDTO.getRoles());
            } else {
                throw new CommonException("login.failed", "用户名或密码错误");
            }
            return loginVO;
        });
    }

    @DeleteMapping("/logout")
    public Map<String, Object> logout() {
        return buildMessage(() -> {
            userService.logout(getToken());
            return null;
        });
    }

    /**
     * 修改密码
     */
    @PostMapping("/password/update")
    public Map<String, Object> updatePassword(String password) {
        return buildMessage(() -> {
            Assert.notNull(password, ErrorMessage.PARAMS_IS_NULL);
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            Teacher update = new Teacher();
            update.setId(teacher.getId());
            update.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
            teacherService.save(update);
            return null;
        });
    }
}
